<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI客服</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }
        header {
            background-color: #4CAF50;
            color: white;
            padding: 15px;
            text-align: center;
        }
        main {
            padding: 20px;
            background: white;
            max-width: 800px;
            margin: auto;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        footer {
            text-align: center;
            padding: 10px 0;
            background-color: #4CAF50;
            color: white;
            position: relative;
            bottom: 0;
            width: 100%;
        }

        .chat-container {
            max-width: 800px;
            margin: 20px auto;
            border: 1px solid #ccc;
            border-radius: 10px;
            overflow: hidden;
        }
        .chat-display {
            padding: 10px;
            background-color: #f9f9f9;
            height: 300px;
            overflow-y: scroll;
        }
        .chat-input {
            display: flex;
            align-items: center;
            padding: 10px;
            background-color: #f5f5f5;
        }
        .chat-input input {
            flex-grow: 1;
            margin-right: 10px;
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .chat-input button {
            padding: 5px 10px;
            background-color: #428bca;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <header>
        <h1>欢迎咨询AI客服</h1>
    </header>
    <main id="chat_content">
        <div class="chat-container">
            <div class="chat-display">
                <!-- 对话展示框将在这里 -->
            </div>
            <div class="chat-input">
                <input type="text" id="message-input" placeholder="请输入内容">
                <button id="submit-btn">发送</button>
            </div>
        </div>
    </main>
    <footer>
        <p>&copy; 2024 二手书交易网站</p>
    </footer>
    <script>
        const messageInput = document.getElementById('message-input');
        const submitBtn = document.getElementById('submit-btn');
        const chatDisplay = document.querySelector('.chat-display');

        submitBtn.addEventListener('click', sendMessage);
        function sendMessage() {
            // 获取输入的消息内容
            const message = messageInput.value;
            const messageAI = '';
            if (message){
                const query = {query : message};
                //发送问题并接收
                fetch('/data', {
                    method: 'POST',
                    body: JSON.stringify(query),
                    headers: {
                        'Content-Type': 'application/json'
                    }
                })
                .then(response => response.json())
                .then(result => {
                    console.log(result);
                    messageAI = result;
                });

                // 创建一个新的消息元素
                const newMessage = document.createElement('div');
                newMessage.classList.add('message');

                const newMessageAI = document.createElement('div');
                newMessageAI.classList.add('messageAI');
                // 设置消息内容和样式
                newMessage.innerHTML = message;
                newMessage.style.backgroundColor = '#428bca';
                newMessage.style.color = 'black';
                newMessage.style.padding = '5px 10px';
                newMessage.style.marginBottom = '10px';

                newMessageAI.innerHTML = messageAI;
                newMessageAI.style.backgroundColor = '#428bca';
                newMessageAI.style.color = 'black';
                newMessageAI.style.padding = '5px 10px';
                newMessageAI.style.marginBottom = '10px';
                // 将消息添加到对话展示框中
                chatDisplay.appendChild(newMessage);
                chatDisplay.appendChild(newMessageAI);
                // 清空输入框
                messageInput.value = '';
            }
        }
    </script>
</body>
</html>